package lib

import (
	"algorithms/ds"
)

func init() {
	Probs = append(Probs, Problem{
		Num:         160,
		Discription: "返回链表相交的节点",
		Level:       1,
		Labels: map[string]int{
			"链表":    1,
			"双指针滑动": 1,
			"循环链表": 1,
		},
	})
}

func GetIntersectionNode(headA, headB *ds.ListNode) *ds.ListNode {
	p1 := headA
	p2 := headB
	//p1走完去headB，p2走完去headA
    //若有交点，p1 p2各走完各自的链表后在第二圈会汇合
	//若没交点，tempA走完len(a)+len(b)后会与走完len(b)+len(a)的tepmB同时为nil
	for p1 != p2 {
		if p1 == nil {
			p1 = headB
		} else {
			p1 = p1.Next
		}

		if p2 == nil {
			p2 = headA
		} else {
			p2 = p2.Next
		}
	}

	return p1
}
